package com.len.controller;

import com.len.base.BaseController;
import com.len.core.annotation.Log;
import com.len.entity.BehaviorField;
import com.len.exception.MyException;
import com.len.service.BehaviorFieldService;
import com.len.service.ReportConfigService;
import com.len.util.BeanUtil;
import com.len.util.JsonUtil;
import com.len.util.ReType;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @author zhuxiaomeng
 * @date 2017/12/19.
 * @email 154040976@qq.com
 * 角色业务
 */
@Controller
@RequestMapping(value = "/behaviorField")
public class BehaviorFieldController extends BaseController {

    @Autowired
    private BehaviorFieldService behaviorFieldService;
    @Autowired
    private ReportConfigService reportConfigService;

    @GetMapping(value = "showBehaviorField")
    @RequiresPermissions(value = "behaviorField:show")
    public String showBehaviorField(Model model) {
        return "behaviorField/behaviorFieldList";
    }

    @ApiOperation(value = "/showBehaviorFieldList", httpMethod = "GET", notes = "展示行为属性")
    @GetMapping(value = "showBehaviorFieldList")
    @ResponseBody
    @RequiresPermissions("behaviorField:show")
    public ReType showBehaviorFieldList(BehaviorField behaviorField, Model model, String page, String limit) {
        return behaviorFieldService.show(behaviorField, Integer.valueOf(page), Integer.valueOf(limit));
    }

    @GetMapping(value = "showAddBehaviorField")
    public String goAddBehaviorField(Model model) {
        return "behaviorField/add-behaviorField";
    }
//
    @ApiOperation(value = "/addBehaviorField", httpMethod = "POST", notes = "添加行为")
    @Log(desc = "添加行为属性")
    @PostMapping(value = "addBehaviorField")
    @ResponseBody
    public JsonUtil addBehaviorField(BehaviorField behaviorField) {
        if (StringUtils.isEmpty(behaviorField.getFieldName())) {
            JsonUtil.error("行为名称不能为空");
        }
        JsonUtil j = new JsonUtil();
        try {
            BehaviorField b = new BehaviorField();
            b.setBehaviorId(behaviorField.getBehaviorId());
            List<BehaviorField> selectBehaviorFieldList = behaviorFieldService.select(b);
            Integer size = selectBehaviorFieldList.size();
            if(size > 0){
                behaviorField.setSortNum(size + 1);
            } else {
                behaviorField.setSortNum(1);
            }
            behaviorFieldService.insertSelective(behaviorField);
            j.setMsg("保存成功");
        } catch (MyException e) {
            j.setMsg("保存失败");
            j.setFlag(false);
            e.printStackTrace();
        }
        return j;
    }

    @GetMapping(value = "updateBehaviorField")
    public String updateBehaviorField(String id, Model model, boolean detail) {
        if (StringUtils.isNotEmpty(id)) {
            BehaviorField behaviorField = behaviorFieldService.selectByPrimaryKey(id);
            model.addAttribute("behaviorField", behaviorField);
        }
        model.addAttribute("detail", detail);
        return "behaviorField/update-behaviorField";
    }

    @ApiOperation(value = "/getBehaviorField", httpMethod = "POST", notes = "通过id获取行为属性信息")
    @PostMapping(value = "getBehaviorField")
    @ResponseBody
    public JsonUtil getBehaviorField(String id) {

        JsonUtil j = new JsonUtil();
        if (StringUtils.isNotEmpty(id)) {
            try {
                BehaviorField behaviorField = behaviorFieldService.selectByPrimaryKey(id);
                j.setMsg("保存成功");
                j.setData(behaviorField);
            } catch (MyException e) {
                j.setMsg("保存失败");
                j.setFlag(false);
                e.printStackTrace();
            }
        }
        return j;
    }

    @ApiOperation(value = "/updateBehaviorField", httpMethod = "POST", notes = "更新行为")
    @Log(desc = "更新行为属性")
    @PostMapping(value = "updateBehaviorField")
    @ResponseBody
    public JsonUtil updateBehaviorField(BehaviorField behaviorField) {
        JsonUtil jsonUtil = new JsonUtil();
        jsonUtil.setFlag(false);
        if (behaviorField == null) {
            jsonUtil.setMsg("获取数据失败");
            return jsonUtil;
        }
        try {
            BehaviorField oldBehaviorField = behaviorFieldService.selectByPrimaryKey(behaviorField.getId());
            BeanUtil.copyNotNullBean(behaviorField, oldBehaviorField);
            behaviorFieldService.updateByPrimaryKeySelective(oldBehaviorField);
            jsonUtil.setFlag(true);
            jsonUtil.setMsg("修改成功");
        } catch (MyException e) {
            jsonUtil.setMsg("修改失败");
            e.printStackTrace();
        }
        return jsonUtil;
    }

    @ApiOperation(value = "/del", httpMethod = "POST", notes = "删除行为属性")
    @Log(desc = "删除行为属性", type = Log.LOG_TYPE.DEL)
    @PostMapping(value = "del")
    @ResponseBody
    @RequiresPermissions("behaviorField:del")
    public JsonUtil del(String id) {
        if (StringUtils.isEmpty(id)) {
            return JsonUtil.error("获取数据失败");
        }
        BehaviorField behaviorField = new BehaviorField();
        behaviorField.setId(id);
        JsonUtil j = new JsonUtil();
        try {
            int count = reportConfigService.selectCountByCondition(behaviorField);
            if (count > 0) {
                return JsonUtil.error("已分配给报表，删除失败");
            }
            behaviorFieldService.deleteByPrimaryKey(id);
            j.setMsg("删除成功");
        } catch (MyException e) {
            j.setMsg("删除失败");
            j.setFlag(false);
            e.printStackTrace();
        }
        return j;
    }

}
